// Extensions Made Easy - Copyright 2012 by Jan Van der Haegen - All rights reserved, use for educational purposes only.

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using ExtensionsMadeEasy.ClientAPI.Theme;
using Microsoft.LightSwitch.Theming;
using Microsoft.VisualStudio.ExtensibilityHosting;

namespace ExtensionsMadeEasy.Presentation.Themes
{
    [Export(typeof(IThemeExtension))]
    [Export(typeof(ITheme))]
    [Theme(EasyTheme.ThemeId, EasyTheme.ThemeVersion)]
    public sealed class EasyTheme : ITheme, IThemeExtension
    { 
        public IThemeExtension ActualThemeExtension { get; private set; }
        public ITheme ActualTheme { get; private set; }

        #region Initialize
        private bool initialized = false;

        private void initialize()
        {
            if (!this.initialized)
            {
                var allThemeExtensionExporters = VsExportProviderService.GetExportedValues<IThemeExporter>();
                var themeExtensionExporter = allThemeExtensionExporters.FirstOrDefault();
                if (themeExtensionExporter != null)
                {
                    this.ActualTheme = themeExtensionExporter.GetTheme();
                }

                if (themeExtensionExporter is IThemeExtensionExporter)
                {
                    this.ActualThemeExtension = (themeExtensionExporter as IThemeExtensionExporter).GetThemeExtension();
                }
                this.initialized = true;
            }
        }
        #endregion
        
        #region ITheme Members

        public string Id
        {
            get
            {
                return EasyTheme.ThemeId;
            }
        }

        public string Version
        {
            get
            {
                return EasyTheme.ThemeVersion;
            }
        }

        public Uri ColorAndFontScheme
        {
            get
            {
                this.initialize();
                return
                      this.ActualTheme == null ? new Uri(@"/ExtensionsMadeEasy.Client;component/Presentation/Themes/EasyTheme.xaml", UriKind.Relative) :
                      this.ActualTheme.ColorAndFontScheme;            
            }
        }

        #endregion

        #region IThemeExtension Members
        public System.Collections.Generic.IEnumerable<System.Uri> GetControlStyleResources(string themeId, string themeVersion, System.Collections.Generic.IEnumerable<Microsoft.LightSwitch.Model.IModuleDefinition> modules)
        {
            this.initialize();
            return this.ActualThemeExtension != null ? this.ActualThemeExtension.GetControlStyleResources(themeId, themeVersion, modules) :
                   this.EmptyList;
        }
        #endregion

        #region Constants

        internal const string ThemeId = "ExtensionsMadeEasy:EasyTheme";
        internal const string ThemeVersion = "1.2";
        internal readonly IList<Uri> EmptyList = new System.Collections.ObjectModel.ReadOnlyCollection<Uri>(new List<Uri>());
        #endregion
    }
}